// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
angular.module('hyt', [
    'ionic',
    'hyt.ngCordovaMocksHdoit',
    'hyt.ngCordovaScan',
    'hyt.ngCordovaGPS',
    'ngCordova',
    'ngResource',
    'hyt.config',
    'hyt.router',
    'hyt.userCtrl',
    'hyt.userService',
    'hyt.manageKaiDanCtrl',
    'hyt.manageKaiDanService',
    'hyt.manageYunDanCtrl',
    'hyt.manageYunDanService',
    'hyt.hzYunDanService',
    'hyt.hzYunDanCtrl',
    'hyt.receiptDaoZhanCtrl',
    'hyt.receiptDaoZhanService',
    'hyt.zddzCtrl',
    'hyt.zddzService',
    'hyt.shrCtrl',
    'hyt.shrService',
    'hyt.zddzService',
    'hyt.findXianLuCtrl',
    'hyt.findXianLuService',
    'hyt.scanCodeCtrl',
    'hyt.khdzService',
    'hyt.khdzCtrl',
    'hyt.partnerdzCtrl',
    'hyt.partnerdzService',
    'hyt.clcxService',
    'hyt.clcxCtrl',
    'hyt.scanCodeService',
    'hyt.menuService',
    'hyt.menuCtrl',
    'hyt.mailListCtrl',
    'hyt.mailListService',
    'hyt.hzKaiDanCtrl',
    'hyt.hzKaiDanService',
    'hyt.hzDuizhangCtrl',
    'hyt.hzdzService'
])

.run(function($ionicPlatform,$rootScope, $location, $timeout, $ionicHistory, $cordovaToast,Storage,Judge,$state,$ionicPopup) {

  $ionicPlatform.ready(function() {
    if(window.cordova && window.cordova.plugins.Keyboard) {
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      // for form inputs)
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

      // Don't remove this line unless you know what you are doing. It stops the viewport
      // from snapping when text inputs are focused. Ionic handles this internally for
      // a much nicer keyboard experience.
      cordova.plugins.Keyboard.disableScroll(true);
        $timeout(function () {
            navigator.splashscreen.hide();
        }, 1000);
    }
    if(window.StatusBar) {
        StatusBar.styleDefault();
    }

  });
  //双击退出
  $ionicPlatform.registerBackButtonAction(function (e) {
      //判断处于哪个页面时双击退出
      var id=Storage.get('cyd_id_back');
      var mc=Storage.get('mc_back');
      var khmc=Storage.get('khmc_back');
      var lxdh=Storage.get('lxdh_back');
      var sflx=Storage.get('sflx_back');
      if ($location.path() == '/menu'||$location.path() == '/login/') {
          if ($rootScope.backButtonPressedOnceToExit) {
              ionic.Platform.exitApp();
          } else {
              $rootScope.backButtonPressedOnceToExit = true;
              $cordovaToast.showShortTop('再按一次退出系统');
              $timeout(function () {
                  $rootScope.backButtonPressedOnceToExit = false;
              }, 2000);
          }
      }else if ($location.path() == '/addcyd/'+mc+'/'+khmc+'/'+lxdh+'/'+sflx){
          var receipt = $ionicPopup.confirm({
              title : '是否退出当前页面',
              cancelText : '取消',
              cancelType : 'button-stable',
              okText : '确定'
          });
          receipt.then(function(r){
              if(r){
                  $state.go("menu");
                  Storage.set('scrollWz',null);
                  Storage.set('cyd',null);
                  Storage.set('fkfs',null);
                  Storage.set('shfs',null);
              }else{
                  console.log('取消');
              }
          });
      } else if ($location.path()=='/cydChange/'+id+'/'+mc+'/'+khmc+'/'+lxdh+'/'+sflx){
          var receipt = $ionicPopup.confirm({
              title : '是否退出当前页面',
              cancelText : '取消',
              cancelType : 'button-stable',
              okText : '确定'
          });
          receipt.then(function(r){
              if(r){
                  $state.go("cydlist");
                  Storage.set('scrollChangeWz',null);
                  Storage.set('cydData',null);
                  Storage.set('fkfs',null);
                  Storage.set('shfs',null);
              }else{
                  console.log('取消');
              }
          });
      }else if ($location.path()=='/cydlist////////'){
          $state.go("menu");
      }else if ($ionicHistory.backView()) {
          $ionicHistory.goBack();
      } else {
          if ($rootScope.backButtonPressedOnceToExit) {
              ionic.Platform.exitApp();
          } else {
              $rootScope.backButtonPressedOnceToExit = true;
              $cordovaToast.showShortTop('再按一次退出系统');
              $timeout(function () {
                  $rootScope.backButtonPressedOnceToExit = false;
              }, 2000);
          }
      }
      e.preventDefault();
      return false;
  }, 101);
    //判断网络连接
    document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() {
        checkConnection();
    }
    function checkConnection() {
        var networkState = navigator.connection.type;

        var states = {};
        states[Connection.UNKNOWN]  = 'Unknown connection';
        states[Connection.ETHERNET] = 'Ethernet connection';
        states[Connection.WIFI]     = 'WiFi connection';
        states[Connection.CELL_2G]  = 'Cell 2G connection';
        states[Connection.CELL_3G]  = 'Cell 3G connection';
        states[Connection.CELL_4G]  = 'Cell 4G connection';
        states[Connection.CELL]     = 'Cell generic connection';
        states[Connection.NONE]     = 'No network connection';

        if(states[networkState]=='No network connection'){
            $state.go('login');
        }
    }
    var sid=Storage.get('sid');
    var user_id=Storage.get('user_id');
    var yh_id=Storage.get('yh_id');
    var sfwlgs=Storage.get('sfwlgs');
    var dw_id=Storage.get('dw_id');
    if(sid==null){
        $state.go('login');
    }else {
        Judge.judgeSid(sid,user_id,yh_id,sfwlgs,dw_id);
        $rootScope.$on('Judge.judgeUpdated', function() {
            var judgeResult=Judge.getJudgeResult();
            Storage.set('sid',judgeResult.SID);
            if(judgeResult.ACTION_RETURN_CODE==000000){
                $state.go('menu');
            }
            else {
                $state.go('login');
            }
        });
    }

})



